package org.hxyjs.dao.impl;

import org.hxyjs.dao.SupplierDao;
import org.hxyjs.entity.Supplier;
import org.hxyjs.mapper.SupplierMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("supplierDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class SupplierDaoImpl implements SupplierDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private SupplierMapper supplierMapper;

    public List<Supplier> getAllSuppliers() throws Exception {
        return supplierMapper.selectByExample(null);
    }

    public List<Supplier> getSupplierByPage(PageInfo pageInfo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) supplierMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return supplierMapper.getSupplierByPage(map);
    }

//    @Override
//    public Supplier selectSupplierBySuno(String sno) throws Exception {
//        return supplierMapper.selectByPrimaryKey(sno);
//    }


    @Override
    public Integer deleteSupplierBySuno(String suno) {
        return supplierMapper.deleteByPrimaryKey(suno);
    }

    @Override
    public Integer updateSupplierBySuno(Supplier supplier) {
        return supplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public Integer addSupplier(Supplier supplier) {
        supplier.setSuno(initId());
        return supplierMapper.insertSelective(supplier);
    }

    @Override
    public Supplier selectSupplierWithorderAndUser(String suno) throws Exception {
        return supplierMapper.selectSupplierWithorderAndUser(suno);
    }

    public String initId() {
        String maxSuno = supplierMapper.getMaxSuno();
        return GenNumTools.initId("", 4, maxSuno);
    }

    @Override
    public Supplier getSupplierBySuno(String suno) throws Exception {
        return supplierMapper.selectByPrimaryKey(suno);
    }

}